/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Administrator
 */
public class ListParkingSlot<E> implements Stack{

    private int count;
    private Node<E> head;
    private int limit;

    public ListParkingSlot(int l){
        head = null;
        count = -1;
        limit = l;
    }

    public void push(Object o) throws StackOverflow{
        StackOverflow overflow = new StackOverflow("Ya no hay espacio para almacenar en el stack");
        Node<E> temp = new Node<E>((E)o,null);

        if(count < limit){
            count++;
            if(head != null){
                Node<E> finger = head;
                while (finger.hasNext())
                    finger = finger.getNext();
                finger.setNext(temp);
            }
            else
                head = temp;
        }
        else
            throw overflow;
    }

    public E peek() throws StackUnderflow{
        StackUnderflow underflow = new StackUnderflow("El stac esta vacio");

        if (count >= 0){
            if (head != null){
                Node<E> finger = head;
                while (finger.hasNext())
                    finger = finger.getNext();
                return((E)finger.getValue());
            }
            else
                return((E)head.getValue());
        }
        else
            throw underflow;
    }

    public E pop() throws StackUnderflow{
        StackUnderflow underflow = new StackUnderflow("El stac esta vacio");

        if (count >= 0){
            if (head != null){
                Node<E> finger = head;
                Node<E> previous = null;
                while (finger.hasNext()){
                    previous = finger;
                    finger = finger.getNext();
                }
                if (previous == null)
                    head = null;
                else
                    previous.setNext(null);
                count--;
                return((E)finger.getValue());
            }
            else
                return(head.getValue());
        }
        else
            throw underflow;
        
    }

    public boolean getEmpty(){
        
        if (head == null)
            return(true);
        return(false);
    }

    public int getIndex(){
        return(count);
    }

}
